//
//  HomeTabVCService Cell.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/10/29.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "ServiceCell.h"
#import "ServiceModel.h"
@interface ServiceCell()
@property (weak, nonatomic) IBOutlet UIView *view;
@property (weak, nonatomic) IBOutlet UIImageView *iv;
@property (weak, nonatomic) IBOutlet UILabel *titleL;
@property (weak, nonatomic) IBOutlet UILabel *resourceL;
@property (weak, nonatomic) IBOutlet UILabel *unitL;
@property (weak, nonatomic) IBOutlet UIImageView *startOneIV;
@property (weak, nonatomic) IBOutlet UIImageView *startTwoIV;
@property (weak, nonatomic) IBOutlet UIImageView *startThreeIV;
@property (weak, nonatomic) IBOutlet UIImageView *startFourIV;
@property (weak, nonatomic) IBOutlet UIImageView *startFiveIV;
@property (weak, nonatomic) IBOutlet UILabel *evaluateL;
@property(nonatomic,assign) int b;
@property (weak, nonatomic) IBOutlet UILabel *billOneL;
@property (weak, nonatomic) IBOutlet UILabel *billTwoL;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *billOneContraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *billTwoConstraint;
@end
static NSString *identifier = @"ServiceCell";
@implementation ServiceCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.view.layer.masksToBounds = YES;
    self.view.layer.cornerRadius = 3;
    self.view.layer.borderColor = kLineColor.CGColor;
    self.view.layer.borderWidth = 1;
    self.evaluateL.textColor = kLabel102Color;
    self.unitL.textColor = kLabel153Color;
    self.unitL.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1];
    self.unitL.layer.masksToBounds = YES;
    self.unitL.layer.cornerRadius = 3;
    self.billOneL.textColor = kLabel153Color;
    self.billOneL.layer.masksToBounds = YES;
    self.billOneL.layer.cornerRadius = 3;
    self.billTwoL.textColor = kLabel153Color;
    self.billTwoL.layer.masksToBounds = YES;
    self.billTwoL.layer.cornerRadius = 3;
    self.iv.layer.borderColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1].CGColor;
    self.iv.layer.borderWidth = 1;
    self.iv.layer.cornerRadius = 6;
    self.iv.layer.masksToBounds = YES;
    self.billOneL.backgroundColor = [UIColor colorWithRed:244/255.0 green:243/255.0 blue:243/255.0 alpha:1];
    self.billTwoL.backgroundColor = [UIColor colorWithRed:244/255.0 green:243/255.0 blue:243/255.0 alpha:1];
//    self.contentView.backgroundColor = kBg;
}

+(instancetype)cellNibWithTableView:(UITableView *)tableView
{
    ServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"ServiceCell" bundle:nil] forCellReuseIdentifier:identifier];
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)setServiceModel:(ServiceModel *)serviceModel
{
    _serviceModel = serviceModel;
    [self.iv sd_setImageWithURL:[NSURL URLWithString:serviceModel.logo] placeholderImage:[UIImage imageNamed:@"noDataImg"]];
    if (serviceModel.typeSub2List.count) {
        if (serviceModel.typeSub2List.count ==1) {
            self.titleL.text = [NSString stringWithFormat:@"%@|%@",serviceModel.associationtype,serviceModel.typeSub2List[0]];
        }else {
            self.titleL.text = [NSString stringWithFormat:@"%@|%@|%@",serviceModel.associationtype,serviceModel.typeSub2List[0],serviceModel.typeSub2List[1]];
        }
    }
    if (serviceModel.type == 1) {
        self.unitL.text = @"个人";
    }else{
        self.unitL.text = @"企业";
    }
    self.evaluateL.text = [NSString stringWithFormat:@"共%ld人评价",serviceModel.evaluateCount];
    self.resourceL.text = [NSString stringWithFormat:@"服务概述:%@",serviceModel.content];
    XLog(@"ServiceModel.evaluateScope:%.2f",serviceModel.evaluateScope);
    if (serviceModel.billingList.count) {
        self.billOneL.text = serviceModel.billingList[0];
        self.billOneContraint.constant = [MCLabel getWidthWithTitle:serviceModel.billingList[0] withFontSize:11]+20;
        if (serviceModel.billingList.count>1) {
        self.billTwoL.text = serviceModel.billingList[1];
            self.billTwoConstraint.constant = [MCLabel getWidthWithTitle:serviceModel.billingList[1] withFontSize:11]+20;
        }else {
            self.billTwoL.hidden = YES;
        }
    }else {
        self.billOneL.hidden = YES;
        self.billTwoL.hidden = YES;
    }
//    if (serviceModel.) {
//        <#statements#>
//    }
    
    int a = 0;
    double c = 0;
    self.b = 0;
    //向下
    a = floor(serviceModel.evaluateScope);
    XLog(@"a = %d",a);
    c = serviceModel.evaluateScope-a;
    c=c*10;
    XLog(@"c = %.0f",c);
    if (c == 0) {
        self.b = serviceModel.evaluateScope;
        [self startCount];
    }else if(c>=5){
        //    向上
        self.b = ceil(serviceModel.evaluateScope);
        [self startCount];
    }else {
        self.b= a;
        [self startHalfCount];//半颗星
    }
    XLog(@"b = %d",self.b);
    //2.1->2
}
-(void)startCount
{
    if (self.b== 0) {
        self.startOneIV.image = [UIImage imageNamed:@"icon_48"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_48"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_48"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_48"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_48"];
    }
    if (self.b>=1) {
        self.startOneIV.image = [UIImage imageNamed:@"icon_43"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_48"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_48"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_48"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_48"];
    }
    if (self.b >=2){
        self.startTwoIV.image = [UIImage imageNamed:@"icon_43"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_48"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_48"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_48"];
    }
    if (self.b >=3){
        self.startThreeIV.image = [UIImage imageNamed:@"icon_43"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_48"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_48"];
    }
    if (self.b >=4){
        self.startFourIV.image = [UIImage imageNamed:@"icon_43"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_48"];
    }
    if(self.b >=5){
        self.startFiveIV.image = [UIImage imageNamed:@"icon_43"];
    }
    
}
-(void)startHalfCount
{

    if (self.b==1) {
        self.startOneIV.image = [UIImage imageNamed:@"icon_43"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_45"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_48"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_48"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_48"];
    }
    if (self.b == 2){
        self.startOneIV.image = [UIImage imageNamed:@"icon_43"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_43"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_45"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_48"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_48"];
    }
    if (self.b ==3){
        self.startOneIV.image = [UIImage imageNamed:@"icon_43"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_43"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_43"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_45"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_48"];
    }
    if (self.b ==4){
        self.startOneIV.image = [UIImage imageNamed:@"icon_43"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_43"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_43"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_43"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_45"];
    }
    
}
@end
